/**
 * 移动端权限管理器
 * @author Wu.Liang
 * @date 2025-01-27
 */

import { useUserStore, useAuthStore } from '@/store'

class PermissionManager {
  static instance = null
  userPermissions = []
  menuPermissions = []

  static getInstance() {
    if (!PermissionManager.instance) {
      PermissionManager.instance = new PermissionManager()
    }
    return PermissionManager.instance
  }

  /**
   * 设置用户权限
   * @param {Array} permissions 权限编码列表
   */
  setUserPermissions(permissions) {
    this.userPermissions = permissions || []
    console.log('权限管理器: 设置用户权限', this.userPermissions)
  }

  /**
   * 设置菜单权限
   * @param {Array} menuPermissions 菜单权限列表
   */
  setMenuPermissions(menuPermissions) {
    this.menuPermissions = menuPermissions || []
    console.log('权限管理器: 设置菜单权限', this.menuPermissions)
  }

  /**
   * 检查是否有菜单权限
   * @param {string} url 页面路径
   * @returns {boolean} 是否有权限
   */
  hasMenuPermission(url) {
    // 暂时允许所有用户访问所有页面
    // TODO: 后续根据具体需求设置权限控制
    console.log('权限管理器: 暂时允许访问所有页面', url)
    return true
  }

  /**
   * 根据路径获取权限编码
   * @param {string} path 页面路径
   * @returns {string} 权限编码
   */
  getPermissionCodeByPath(path) {
    const pathPermissionMap = {
      // 第一层级权限
      '/pages/index/index': 'mobile:home:menu',
      '/pages/services/services': 'mobile:service:menu',
      '/pages/profile/index': 'mobile:profile:menu',
      
      // 第二层级权限
      '/pages/security/security': 'mobile:security:menu',
      '/pages/property/property': 'mobile:property:menu',
      '/pages/parking/parking': 'mobile:parking:menu',
      '/pages/emergency/emergency': 'mobile:emergency:menu',
      '/pages/community/community': 'mobile:community:menu',
      '/pages/analytics/analytics': 'mobile:analytics:menu',
      '/pages/user/user': 'mobile:user:menu',
      '/pages/more/more': 'mobile:more:menu',
      
      // 物业服务子页面
      '/pages/property/repair': 'mobile:repair:menu',
      '/pages/property/payment': 'mobile:payment:menu',
      '/pages/property/notice': 'mobile:notice:menu',
      '/pages/complaint/complaint': 'mobile:complaint:menu',
      '/pages/complaint/detail': 'mobile:complaint:detail:menu',
      
      // 安防管理子页面
      '/pages/security/quick': 'mobile:quick:menu',
      '/pages/security/visitor': 'mobile:visitor:menu',
      '/pages/security/access-record': 'mobile:access:record:menu',
      '/pages/security/alarm-info': 'mobile:alarm:info:menu',
      
      // 我的模块页面
      '/pages/profile/edit': 'mobile:profile:edit:menu',
      '/pages/profile/verification': 'mobile:verification:menu',
      '/pages/profile/security': 'mobile:security-settings:menu',
      '/pages/profile/change-password': 'mobile:change-password:menu',
      '/pages/service/repair': 'mobile:my-repair:menu',
      '/pages/service/payment': 'mobile:my-payment:menu',
      '/pages/service/appointment': 'mobile:my-appointment:menu',
      '/pages/household/list': 'mobile:household:menu',
      
      // 公共页面
      '/pages/login/login': 'mobile:login:menu',
      '/pages/register/register': 'mobile:register:menu'
    }

    return pathPermissionMap[path]
  }

  /**
   * 检查是否是超级管理员
   * @returns {boolean} 是否是超级管理员
   */
  isSuperAdmin() {
    const userStore = useUserStore()
    const authStore = useAuthStore()
    
    // 检查用户角色
    const userRoles = userStore.userRoles || []
    const authRoles = authStore.roles || []
    
    const allRoles = [...userRoles, ...authRoles]
    
    return allRoles.includes('SUPER_ADMIN') || 
           allRoles.includes('super_admin') || 
           allRoles.includes('超级管理员')
  }

  /**
   * 获取用户权限列表
   * @returns {Array} 权限列表
   */
  getUserPermissions() {
    const userStore = useUserStore()
    const authStore = useAuthStore()
    
    let userPermissions = userStore.userPermissions || []
    if (!userPermissions || userPermissions.length === 0) {
      userPermissions = authStore.permissions || []
    }
    
    return userPermissions
  }

  /**
   * 检查是否有指定权限
   * @param {string} permissionCode 权限编码
   * @returns {boolean} 是否有权限
   */
  hasPermission(permissionCode) {
    // 超级管理员拥有所有权限
    if (this.isSuperAdmin()) {
      return true
    }

    const userPermissions = this.getUserPermissions()
    return userPermissions.includes(permissionCode)
  }

  /**
   * 清除权限缓存
   */
  clearPermissions() {
    this.userPermissions = []
    this.menuPermissions = []
    console.log('权限管理器: 清除权限缓存')
  }
}

export default PermissionManager
